"""
Problem 27: https://projecteuler.net/problem=27

Euler discovered the remarkable quadratic formula:
n2 + n + 41
It turns out that the formula will produce 40 primes for the consecutive values
n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible
by 41, and certainly when n = 41, 412 + 41 + 41 is clearly divisible by 41.
The incredible formula  n2 − 79n + 1601 was discovered, which produces 80 primes
for the consecutive values n = 0 to 79. The product of the coefficients, −79 and
1601, is −126479.
Considering quadratics of the form:
n² + an + b, where |a| < 1000 and |b| < 1000
where |n| is the modulus/absolute value of ne.g. |11| = 11 and |−4| = 4
Find the product of the coefficients, a and b, for the quadratic expression that
produces the maximum number of primes for consecutive values of n, starting with
n = 0.
"""

# _*_ conding:UTF-8 _*_
'''
@author = Kuperain
@email = kuperain@aliyun.com
@IDE = VSCODE Python3.8.3
@creat_time = 2022/5/10
'''


def isPrime(n: int) -> bool:
    '''
    >>> assert all(list(map(isPrime, [2,3,5,7,11,13])))
    >>> assert not any(list(map(isPrime, [0,1,4,6,8,9])))
    '''

    if n <= 1:
        return False
    if n == 2:
        return True

    n_sqrt = int(n**0.5)

    for i in range(2, n_sqrt+1):
        if n % i == 0:
            return False
    return True


def primesNum(a: int, b: int) -> int:
    '''
    >>> print(primesNum(1,41))
    40
    >>> print(primesNum(-79,1601))
    80
    '''

    i = 0
    while isPrime(i**2 + a*i + b):
        i += 1

    return i


def solution(alimit: int = 1000, blimit: int = 1000) -> tuple:
    '''
    n² + an + b, where |a|< 1000 and |b| < 1000
    Find the product of the coefficients, a and b, for the quadratic expression that
    produces the maximum number of primes for consecutive values of n, starting with
    n = 0.
    '''
    res, res_a, res_b = 0, 0, 0

    for a in range(-alimit+1, alimit):
        for b in range(-blimit+1, blimit):
            tmp = primesNum(a, b)
            if tmp > res:
                res, res_a, res_b = tmp, a, b
                # print(res, res_a, res_b)
    return res, res_a, res_b


if __name__ == "__main__":
    import doctest
    doctest.testmod(verbose=False)

    print(solution())
    # (71, -61, 971)
